﻿using PJ.Model;
using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;

namespace PJ.IRepository
{
    public interface IRepository<TEntity> where TEntity : class
    {
        /// <summary>
        /// 新增单条
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        Task<int> Add(TEntity model);

        /// <summary>
        /// 新增多条
        /// </summary>
        /// <param name="models"></param>
        /// <returns></returns>
        Task<int> Add(List<TEntity> models);

        /// <summary>
        /// 根据主键删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        Task<bool> Delete(object id);

        /// <summary>
        /// 根据条件删除
        /// </summary>
        /// <param name="whereExpression"></param>
        /// <returns></returns>
        Task<bool> Delete(Expression<Func<TEntity, bool>> whereExpression);

        /// <summary>
        /// 修改所有字段
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        void Update(TEntity model);

        /// <summary>
        /// 修改指定字段
        /// </summary>
        /// <param name="whereExpression"></param>
        /// <param name="entity"></param>
        /// <returns></returns>
        Task<bool> Update(TEntity entity, params string[] updateColumnNames);

        /// <summary>
        /// 获取所有实体
        /// </summary>
        /// <returns></returns>
        Task<List<TEntity>> Query();

        /// <summary>
        /// 根据条件获取实体
        /// </summary>
        /// <param name="whereExpression"></param>
        /// <returns></returns>
        Task<List<TEntity>> Query(Expression<Func<TEntity, bool>> whereExpression);

        /// <summary>
        /// 根据条件查询并排序
        /// </summary>
        /// <param name="whereExpression"></param>
        /// <param name="orderByExpression"></param>
        /// <param name="isAsc"></param>
        /// <returns></returns>
        Task<List<TEntity>> Query(Expression<Func<TEntity, bool>> whereExpression, Expression<Func<TEntity, object>> orderByExpression, bool isAsc = true);

        /// <summary>
        /// 分页
        /// </summary>
        /// <param name="whereExpression"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="orderByExpression"></param>
        /// <returns></returns>
        Task<PageModel<TEntity>> QueryPage(Expression<Func<TEntity, bool>> whereExpression, Expression<Func<TEntity, object>> orderByExpression, bool isAsc = true, int pageIndex = 1, int pageSize = 20);

        /// <summary>
        /// 查询记录数
        /// </summary>
        /// <param name="whereExpression"></param>
        /// <returns>记录数</returns>
        Task<int> Count(Expression<Func<TEntity, bool>> whereExpression);

        /// <summary>
        /// 是否存在
        /// </summary>
        /// <param name="whereExpression"></param>
        /// <returns>布尔值</returns>
        Task<bool> Exist(Expression<Func<TEntity, bool>> whereExpression);
    }
}
